<?php
/**
 * Created by PhpStorm.
 * User: 王凯林
 * Date: 18/3/25
 * Time: 14:15
 */

namespace App\Http\Controllers\H5\Shop;


use App\Common\Exceptions\AixinException;
use App\Common\Exceptions\AixinExceptionCode;
use App\Http\Controllers\Controller;
use App\Model\MoneyReport;
use App\Model\Scores;
use App\Service\PeakReportService;
use Illuminate\Http\Request;

class MoneyReportController extends Controller
{
    public $peakReportService;

    public function __construct(PeakReportService $peakReportService)
    {
        $this->peakReportService = $peakReportService;
    }
    /*
     * 个人中心用户余额和积分
     */
    public function index(Request $request){
        //参数验证
        if($request->has('uid')){
            //操作会员财务报表money_report
            $balance = MoneyReport::where('uid',$request->uid)->sum('amount');
            $integral = Scores::where('uid',$request->uid)->sum('bill');
            //控制余额小于0的情况;
            if($balance<0){
                $balance = 0;
            }
            //控制积分小于0的情况;
            if($integral<0){
                $integral = 0;
            }
            //调用用户的复投信息
            $doubleInfo = $this->peakReportService->creditsAndTimes($request->uid);
            $data = [
                'balance'=>$balance,
                'integral'=>$integral,
                'doubleinfo'=>$doubleInfo
            ];
            return ApiResponse($data);
        }else{
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
    }
    /*
     * 个人中心收支明细表
     */
    public function amountList(Request $request){
        //参数验证
        if($request->has('uid') && $request->has('start')&& $request->has('limit')){
            //根据用户的id去money_report表当中查询该用户的流水记录
           $list = MoneyReport::where('uid',$request->uid)->orderby('create_time','desc')->offset($request->start)->limit($request->limit)->get();
           if($list){
               return ApiResponse($list);
           }else{
               return ApiResponse('','miss',1);
           }
        }else{
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
    }

    /*
     * 单条收支详情
     */
    public function amountDetail(Request $request){
        if($request->has('id')){
            //使用门面方法找到id对应的记录详情
            $detail = MoneyReport::where('id',$request->id)->first();
            if($detail){
                return ApiResponse($detail);
            }else{
                return ApiResponse('','miss',1);
            }
        }else{
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }

    }

}